﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.Model;
using iSun.Entity;

namespace iSun.BLL
{
    /// <summary>
    /// 角色的业务逻辑类
    /// 
    /// </summary>
    public partial class BRole : BLL.Base.BaseBussnies<SysRole>
    {
        public BRole()
        {
            this.entity = new SysRole();
        }
        /// <summary>
        /// 获取ROLE LIST
        /// </summary>
        /// <returns></returns>
        public IList<SysRole> GetRoles()
        {
            return this.GetEntityListByCondition<SysRole>("");
        }

        /// <summary>
        /// 通过ROLEID获取ROLE实例
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public SysRole GetRoleByID(Guid RoleID)
        {
            return this.GetEntityById<SysRole>(RoleID);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public IList<DatUser> GetUserRoles(Guid RoleID)
        {
            IList<DatUser> uList = this.GetEntitysBySqlComm("select * from DatUser where RoleID in (select RoleID from SysUserRole where roleid='" + RoleID.ToString() + "')") as IList<DatUser>;

            IList<SysUserRole> list = this.GetEntitysByCondition(" RoleID=") as IList<SysUserRole>;
            IList<String> UserIDArr = list.Select(p => p.UserID.ToString()).ToArray();
           return uList;
            //return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public override string  CheckData(BaseEntity instance)
        {
            SysRole entity = instance as SysRole;
            if (string.IsNullOrEmpty(entity.RoleName))
            {
                return "角色名称不能为空!";
            }
            if (IsExeists(entity))
            {
                return "该角色名称己存在!";
            }
            return base.CheckData(entity);
        }



        public override string CheckData()
        {
            return CheckData(this.entity);
        }

        public bool IsExeists(SysRole entity)
        {
            return GetEntitysByCondition("RoleName ='" + entity.RoleName + "'")!=null;
        }

        public static BRole Instance
        {
            get
            {
                return new BRole();
            }
        }
    }
}
